package com.merelyb.business.service.account;

import com.merelyb.business.service.token.BusinessTokenInfoService;
import com.merelyb.data.service.service.mybatis.bean.account.AccountBasic;
import com.merelyb.data.service.service.mybatis.bean.account.AccountInfo;
import com.merelyb.data.service.service.mybatis.bean.token.TokenInfo;
import com.merelyb.data.service.service.service.account.AccountBasicService;
import com.merelyb.data.service.service.service.account.AccountInfoService;
import com.merelyb.util.MD5Util;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by LiM on 2017/12/11.
 */
@Service
public class BusinessAccountInfoService {
    @Autowired
    AccountInfoService accountInfoService;
    @Autowired
    BusinessTokenInfoService businessTokenInfoService;
    @Autowired
    AccountBasicService accountBasicService;

    /**
     * 新增用户
     * @param sAccount
     * @param sPwd
     * @return
     */
    public int addNewAccountInfo(String sAccount, String sPwd){
        int iReturn = 0;

        try {
            AccountInfo accountInfo = new AccountInfo();
            if(NumberUtils.isNumber(sAccount)){
                accountInfo.setTelNum(NumberUtils.toLong(sAccount));
            }else {
                if(sAccount.contains("@")){
                    accountInfo.setEmail(sAccount);
                }else
                    accountInfo.setUserName(sAccount);
            }
            accountInfo.setIsDelete(0);
            List<AccountInfo> accountInfoList = accountInfoService.queryAllList(accountInfo);
            if (accountInfoList != null && accountInfoList.size() > 0) {
                iReturn = 1;
            } else {
                accountInfo.setUserPwd(MD5Util.string2MD5(sPwd));
                accountInfo.setCreateTime(new Date());
                accountInfoService.addData(accountInfo);
            }
        } catch (Exception e) {
            iReturn = -1;
            e.printStackTrace();
        } finally {
            return iReturn;
        }
    }

    /**
     * 检查用户是否存在并返回token
     * @param sAccount
     * @param sPwd
     * @return
     */
    public TokenInfo checkAccount(String sAccount, String sPwd){
        TokenInfo tokenInfoResult = null;
        try {
            AccountInfo accountInfo = new AccountInfo();
            if(NumberUtils.isNumber(sAccount)){
                accountInfo.setTelNum(NumberUtils.toLong(sAccount));
            }else {
                if(sAccount.contains("@")){
                    accountInfo.setEmail(sAccount);
                }else
                    accountInfo.setUserName(sAccount);
            }
            accountInfo.setUserPwd(MD5Util.string2MD5(sPwd));
            accountInfo.setIsDelete(0);
            List<AccountInfo> accountInfoList = accountInfoService.queryAllList(accountInfo);
            if (accountInfoList != null && accountInfoList.size() == 1) {
                AccountInfo account = accountInfoList.get(0);
                tokenInfoResult = new TokenInfo();
                tokenInfoResult.setAccId(account.getId());
                tokenInfoResult = businessTokenInfoService.checkTokenInfo(tokenInfoResult);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            return tokenInfoResult;
        }
    }

    /**
     * 获取账户信息
     * @param sAccount
     * @return
     */
    public AccountInfo getAccountInfo(String sAccount){
        AccountInfo accountInfoReturn = null;
        try {
            AccountInfo accountInfo = new AccountInfo();
            if(NumberUtils.isNumber(sAccount)){
                accountInfo.setTelNum(NumberUtils.toLong(sAccount));
            }else {
                if(sAccount.contains("@")){
                    accountInfo.setEmail(sAccount);
                }else
                    accountInfo.setUserName(sAccount);
            }
            accountInfo.setIsDelete(0);
            List<AccountInfo> accountInfoList = accountInfoService.queryAllList(accountInfo);
            if (accountInfoList != null && accountInfoList.size() == 1) {
                accountInfoReturn = accountInfoList.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return accountInfoReturn;
        }
    }


    /**
     * 新增账户基本信息
     * @param accountBasic
     * @return
     */
    public boolean addNewAccountBasic(AccountBasic accountBasic){
        boolean bReturn = false;
        try {
            accountBasicService.addData(accountBasic);
            bReturn = true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return bReturn;
        }
    }

    /**
     * 更新账户基本信息
     * @param accountBasic
     * @return
     */
    public boolean updateAccountBasic(AccountBasic accountBasic){
        boolean bReturn = false;
        try {
            accountBasicService.updateData(accountBasic);
            bReturn = true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return bReturn;
        }
    }

    /**
     * 更新账户信息状态
     * @param accountBasic
     * @return
     */
    public boolean updateAccountBasicState(AccountBasic accountBasic){
        boolean bReturn = false;
        try {
            accountBasicService.deleteData(accountBasic);
            bReturn = true;
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return bReturn;
        }
    }

    /**
     * 查询所有数据
     * @param accountBasic
     * @return
     */
    public List<AccountBasic> queryAccountBasic(AccountBasic accountBasic){
        List<AccountBasic> accountBasicList = new ArrayList<>();
        try {
            accountBasicList = accountBasicService.queryAllList(accountBasic);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return accountBasicList;
        }
    }

    /**
     * 分页查询数据
     * @param accountBasic
     * @return
     */
    public List<AccountBasic> queryAccountBasicByPage(AccountBasic accountBasic){
        List<AccountBasic> accountBasicList = new ArrayList<>();
        try {
            accountBasicList = accountBasicService.queryListByLimit(accountBasic);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            return accountBasicList;
        }
    }
}
